package mapReduce.demo03_compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WordCountCompressApp {

    public static class WCCMapper extends Mapper<LongWritable, Text,Text, IntWritable>{

        Text outKey = new Text();
        IntWritable outValue = new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] words = line.split(" ");
            for (String word : words) {
                outKey.set(word);
                context.write(outKey,outValue);
            }
        }
    }

//    public static class WCCReducer extends Reducer<Text,IntWritable,Text, NullWritable>{
//
//        Text outKey = new Text();
//
//        @Override
//        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
//
//            int sum = 0;
//            for (IntWritable value : values) {
//                sum+=value.get();
//            }
//
//            outKey.set(key.toString()+"\t"+sum);
//
//            context.write(outKey,NullWritable.get());
//
//        }
//    }


    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();

        //        propMap.put("io.compression.codecs",
//                "org.apache.hadoop.io.compress.DefaultCodec,"
//                + "org.apache.hadoop.io.compress.GzipCodec,"
//                + "org.apache.hadoop.io.compress.BZip2Codec,"
//                + "org.apache.hadoop.io.compress.Lz4Codec,"
//                + "org.apache.hadoop.io.compress.SnappyCodec");

        //设置map之后，reduce之前是否开启压缩机制，并设置压缩的文件格式
//        conf.set("mapreduce.map.output.compress","true");
//        conf.set("mapreduce.map.output.compress.codec","org.apache.hadoop.io.compress.GzipCodec");

        //设置reduce之后是否开启压缩机制，并设置压缩的文件格式
        conf.set("mapreduce.output.fileoutputformat.compress","true");
        conf.set("mapreduce.output.fileoutputformat.compress.codec","org.apache.hadoop.io.compress.GzipCodec");
        conf.set("mapreduce.output.fileoutputformat.compress.type","RECORD");

        Job job = Job.getInstance(conf);

        //4 四个输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //3 三个类
        job.setJarByClass(WordCountCompressApp.class);
        job.setMapperClass(WCCMapper.class);
//        job.setReducerClass(WordCountReducer.class);

//        job.setNumReduceTasks(0);

        //创建一个文件系统的连接
        FileSystem fs = FileSystem.get(conf);

        //设置一个输出路径的公共变量
        Path outputPath = new Path(args[1]);
        //如果文件存在就删除
        if (fs.exists(outputPath)) {
            fs.delete(outputPath,true);
        }

        //2 两个路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,outputPath);

        //1 一次提交任务
        job.waitForCompletion(true);

    }


}
